package xcmg.device.service.setting;

import com.github.pagehelper.PageInfo;
import yb.ecp.fast.infra.util.PageHelperPlus;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import xcmg.device.dao.entity.setting.AppSettingDO;
import xcmg.device.dao.mapper.AppSettingMapper;
import xcmg.device.infra.BeanUtil;
import xcmg.device.infra.ErrorCode;
import xcmg.device.infra.FileUtil;
import xcmg.device.service.vo.setting.AppSettingVO;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by sqq on 2018/3/14.
 */
@Service
public class AppSettingService {

    @Autowired
    private AppSettingMapper appSettingMapper;

    @Autowired
    private FastGenClient fastGenClient;


    @Autowired
    private HttpServletResponse response;

    @Value("${spring.file.download.path}")
    private String filePath;

    public ErrorCode insert(AppSettingVO appSettingVO) {
        Integer versionCount = appSettingMapper.versionCount(appSettingVO.getType(), appSettingVO.getVersion());
        if (versionCount != null && versionCount > 0) {
            return ErrorCode.VersionExists;
        }
        AppSettingVO latestItem = itemLatestByType(appSettingVO.getType());
        if (latestItem != null && latestItem.getVersion().compareTo(appSettingVO.getVersion()) >= 0) {
            return ErrorCode.VersionLow;
        }
        ActionResult<String> textResult = fastGenClient.textGuid();
        if (textResult.getCode() != ErrorCode.Success.getCode()) {
            return ErrorCode.IllegalArument;
        }
        AppSettingDO appSettingDO = new AppSettingDO();
        BeanUtils.copyProperties(appSettingVO, appSettingDO);
        appSettingDO.setId(textResult.getValue());
        return appSettingMapper.insert(appSettingDO) > 0 ? ErrorCode.Success : ErrorCode.Failure;
    }

    public AppSettingVO item(String id) {
        AppSettingDO appSettingDO = appSettingMapper.itemByPrimaryKey(id);
        if (appSettingDO == null) {
            return null;
        }
        AppSettingVO appSettingVO = new AppSettingVO();
        BeanUtils.copyProperties(appSettingDO, appSettingVO);
        return appSettingVO;
    }

    public AppSettingVO itemByType() {
        AppSettingDO appSettingDO = appSettingMapper.itemByPrimaryType("PDA");
        if (appSettingDO == null) {
            return null;
        }
        AppSettingVO appSettingVO = new AppSettingVO();
        BeanUtils.copyProperties(appSettingDO, appSettingVO);
        return appSettingVO;
    }

    public ErrorCode update(AppSettingVO appSettingVO) {
        if (appSettingVO == null) {
            return ErrorCode.IllegalArument;
        }
        if (StringUtils.isEmpty(appSettingVO.getId())) {
            return ErrorCode.IllegalArument;
        }
        AppSettingDO appSettingDO = appSettingMapper.itemByPrimaryKey(appSettingVO.getId());
        if (appSettingDO == null) {
            return ErrorCode.IllegalArument;
        }
        appSettingDO.setName(appSettingVO.getName());
        appSettingDO.setIsRigidUpdate(appSettingVO.getIsRigidUpdate());
        appSettingDO.setUpdateUrl(appSettingVO.getUpdateUrl());
        appSettingDO.setNote(appSettingVO.getNote());
        appSettingDO.setFileName(appSettingVO.getFileName());
        appSettingDO.setIsApproval(appSettingVO.getIsApproval());
        return appSettingMapper.update(appSettingDO) > 0 ? ErrorCode.Success : ErrorCode.Failure;
    }

    public PageCommonVO<AppSettingVO> list(SearchCommonVO<AppSettingVO> condition) {
        PageCommonVO<AppSettingVO> result = new PageCommonVO<>();
        if (StringUtils.isEmpty(condition.getSort())) {
            condition.setSort("create_date desc");
        }
        PageHelperPlus.orderBy(condition.getSort());
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        List<AppSettingDO> doList = appSettingMapper.list(condition.getFilters());
        List<AppSettingVO> voList = new ArrayList<>();
        for (AppSettingDO appDO : doList) {
            AppSettingVO appSettingVO = BeanUtil.do2bo(appDO, AppSettingVO.class);
            if (appSettingVO != null && null != appSettingVO.getIsRigidUpdate()) {
                appSettingVO.setStatus(appSettingVO.getIsRigidUpdate() ? "是" : "否");
            }
            voList.add(appSettingVO);
        }
        result.setPageInfo(new PageInfo<AppSettingDO>(doList));
        result.setPageInfoList(voList);
        return result;
    }

    public void download(String fileName) throws Exception {
        FileUtil.downLoadFile(response, filePath + "apkk/", fileName);
    }

    public String uploadFile(MultipartFile file, String fileName) throws Exception {
        fileName = fileName + ".apk";
        FileUtil.uploadFile(file, filePath + "apkk/", fileName);
        return fileName;
    }

    public AppSettingVO itemLatestByType(String type) {
        AppSettingDO appSettingDO = appSettingMapper.itemByPrimaryType(type);
        AppSettingVO appSettingVO = new AppSettingVO();
        if (appSettingDO != null) {
            BeanUtils.copyProperties(appSettingDO, appSettingVO);
            return appSettingVO;
        }
        return null;
    }
}
